package com.addcn.caruimodule.text;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;

import com.addcn.caruimodule.R;


/**
 * author:LuWenbo
 * date: On 2019/3/6
 * content: 自帶刪除圖示輸入框
 * describe:
 */
public class CleanEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
    //EditText右側的刪除按鈕
    private Drawable mClearDrawable;
    private boolean hasFoucs;

    public CleanEditText(Context context) {
        this(context, null);
    }

    public CleanEditText(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public CleanEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
// 獲取EditText的DrawableRight,假如沒有設定我們就使用預設的圖片,獲取圖片的順序是左上右下（0,1,2,3,）
        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
            mClearDrawable = getResources().getDrawable(
                    R.drawable.ic_nav_0a_close_24dp);
        }

        mClearDrawable.setBounds(0, 0, (int) (mClearDrawable.getIntrinsicWidth()/1.7),
                (int) (mClearDrawable.getIntrinsicHeight()/1.7));
// 預設設定隱藏圖示
        setClearIconVisible(false);
// 設定焦點改變的監聽
        setOnFocusChangeListener(this);
// 設定輸入框裡面內容發生改變的監聽
        addTextChangedListener(this);
    }

    /* @說明：isInnerWidth, isInnerHeight為ture，觸控點在刪除圖示之內，則視為點選了刪除圖示
     * event.getX() 獲取相對應自身左上角的X座標
     * event.getY() 獲取相對應自身左上角的Y座標
     * getWidth() 獲取控制元件的寬度
     * getHeight() 獲取控制元件的高度
     * getTotalPaddingRight() 獲取刪除圖示左邊緣到控制元件右邊緣的距離
     * getPaddingRight() 獲取刪除圖示右邊緣到控制元件右邊緣的距離
     * isInnerWidth:
     *  getWidth() - getTotalPaddingRight() 計算刪除圖示左邊緣到控制元件左邊緣的距離
     *  getWidth() - getPaddingRight() 計算刪除圖示右邊緣到控制元件左邊緣的距離
     * isInnerHeight:
     *  distance 刪除圖示頂部邊緣到控制元件頂部邊緣的距離
     *  distance + height 刪除圖示底部邊緣到控制元件頂部邊緣的距離
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                int x = (int)event.getX();
                int y = (int)event.getY();
                Rect rect = getCompoundDrawables()[2].getBounds();
                int height = rect.height();
                int distance = (getHeight() - height)/2;
                boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
                boolean isInnerHeight = y > distance && y <(distance + height);
                if (isInnerWidth && isInnerHeight) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 當ClearEditText焦點發生變化的時候，
     * 輸入長度為零，隱藏刪除圖示，否則，顯示刪除圖示
     */
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFoucs = hasFocus;
        if (hasFocus) {
            setClearIconVisible(getText().length() > 0);
        } else {
            setClearIconVisible(false);
        }
    }

    protected void setClearIconVisible(boolean visible) {
        Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        if (hasFoucs) {
            setClearIconVisible(s.length() > 0);
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
